//index.js
import utils from '../../utils/utils';
//获取应用实例
Page({
  data: {
    signDate: [],
    isSign: 'no',
    calendarSignDay: 0, //签到天数
  },
  //签到函数
  calendarSign() {
    let _day = new Date().getDate(); //获取当前天
    let _month = new Date().getMonth() + 1;
    let sign = wx.getStorageSync('signDate')[_month + 'm'] || this.data.date;
    for(let i = 0; i < sign.length; i++) {
      if(this.data.date[i] === _day) {
        sign[i] = 'yes';
      }
    }
    wx.showToast({
      title: '签到成功',
      icon: 'success',
      duration: 2000,
    });
    this.setData({
      signDate: sign,
      isSign: 'yes',
      calendarSignDay: this.data.calendarSignDay + 1
    });
    let _json = {};
    _json[_month + 'm'] = sign;
    wx.setStorageSync('signDate', _json);
  },
  onLoad: function () {
    var mydate = new Date();
    var year = mydate.getFullYear();
    var month = mydate.getMonth() + 1;
    let _day = mydate.getDate();
    this.setData({
      year: year,
      month: month,
      date: utils.getMonthList(year, month), //日历表格
    });
    if(wx.getStorageSync('signDate')) {
      let sign = wx.getStorageSync('signDate')[month + 'm'] || [];
      let isSign = 'no';
      let count = 0;
      for (let i = 0; i < sign.length; i++) {
        if(_day == this.data.date[i]) {
          if(sign[i] === 'yes') {
            isSign = 'yes';
          }
        }
        if(sign[i] === 'yes') {
          count += 1;
        }
      }
      this.setData({
        signDate: sign,
        isSign: isSign,
        calendarSignDay: count,
      });
    }
  }
})